package org.jeecg.modules.nongjt.area.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.nongjt.area.entity.NjtArea;
import org.jeecg.modules.nongjt.area.service.INjtAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 地区层级关系表 前端控制器
 * </p>
 *
 * @author tianshao
 * @since 2022-06-16
 */
@RestController
@RequestMapping("/njt-area")
public class NjtAreaController {
    @Autowired
    private INjtAreaService iNjtAreaService;
    /**
     * 查询所有根节点
     *
     * @return
     */
    @GetMapping(value = "/listRoot")
    public Result<?> listRoot() {
        // 查询所有根节点
        QueryWrapper<NjtArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("parent_code");
        queryWrapper.orderByAsc("code");
        List<NjtArea> areaList = iNjtAreaService.list(queryWrapper);
        for (NjtArea area : areaList) {
            // 根节点默认有子节点
            area.setHasChildren(true);
        }
        return Result.OK(areaList);
    }

    /**
     * 根据父节点Code 查询所有子节点
     *
     * @return
     */
    @GetMapping(value = "/listChildren")
    public Result<?> listChildren(@RequestParam String parentCode) {
        // 查询所有根节点,
        QueryWrapper<NjtArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_code", parentCode);
        queryWrapper.orderByAsc("code");
        List<NjtArea> areaList = iNjtAreaService.list(queryWrapper);
        for (NjtArea area : areaList) {
            // 单位小于2时，默认有子节点，因为unit值为2的乡镇/街道没有子节点
            if (area.getUnit() < 2) {
                //让当前节点显示折叠符号（+）
                area.setHasChildren(true);
            }
        }
        return Result.OK(areaList);
    }

}

